package com.aiti.lulian.service.newCard;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.constant.NewCardBusinessConstant;
import com.aiti.lulian.dto.newCard.NewCardUserVersionInfoDto;
import com.aiti.lulian.mapper.newCard.NewCardVersionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * description:
 * auth fcb
 * date 2025-08-15 14:25
 **/
@Service
@Slf4j
public class CardVersionService {

    @Autowired
    private NewCardVersionMapper newCardVersionMapper;

    /**
     * 查询用户是否开通过试用版
     * true开通过
     * @param userId
     * @return
     */
    public Boolean queryTryUserCount(String userId) {
        // 查询用户当前版本是否已经是付费版，无需再试用
        NewCardUserVersionInfoDto userVersionInfo = newCardVersionMapper.getUserVersionInfo(userId);
        if(StrUtil.isNotEmpty(userVersionInfo.getCardVersion())) {
            if(NewCardBusinessConstant.feeVersionList.contains(userVersionInfo.getCardVersion())) {
                throw new BusinessException("您已开通付费版，无需试用");
            }
        }
        // 0:个人试用版，1：企业试用版
        return newCardVersionMapper.queryTryUserCount(userId, 0) > 0;
    }

    /**
     * 插入用户试用用户
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertTryUser(String userId) {
        this.setUserVersion(userId, NewCardBusinessConstant.CARD_VERSION_2, 7, "day");
        newCardVersionMapper.insertTryUser(IdUtil.simpleUUID(), userId, 0);
    }

    /**
     * 插入企业用户试用用户
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertEnterpriseTryUser(String userId) {
        this.setUserVersion(userId, NewCardBusinessConstant.CARD_VERSION_2, 7, "day");
        newCardVersionMapper.insertTryUser(IdUtil.simpleUUID(), userId, 1);
    }

    /**
     * 设置用户版本
     */
    @Transactional(rollbackFor = Exception.class)
    public void setUserVersion(String userId, String cardVersion, Integer offset, String unit) {
        if("day".equals(unit)) {
            Date expireDate = DateUtil.offsetDay(new Date(), offset);
            newCardVersionMapper.updateUserVersion(userId, cardVersion, expireDate);
            return;
        }
        throw new BusinessException("暂不支持该时间单位");
    }

    /**
     * 获取用户版本信息
     */
    public NewCardUserVersionInfoDto getUserVersionInfo(String userId) {
        NewCardUserVersionInfoDto userVersionInfo = newCardVersionMapper.getUserVersionInfo(userId);
        if(StrUtil.isEmpty(userVersionInfo.getCardVersion())) {
            userVersionInfo.setCardVersion(NewCardBusinessConstant.CARD_VERSION_1);
        }
        if(StrUtil.isBlank(userVersionInfo.getExpireDate())) {
            userVersionInfo.setOverdue(false);
        }else {
            DateTime expireDate = DateUtil.parse(userVersionInfo.getExpireDate());
            if(expireDate.isBefore(DateUtil.date())) {
                userVersionInfo.setOverdue(true);
            }else {
                userVersionInfo.setOverdue(false);
            }
        }

        return userVersionInfo;
    }

    /**
     * 更新名片使用资源类型
     * @param cardId
     * @param useResourceType
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCardUseResourceType(String cardId, Integer useResourceType) {
        newCardVersionMapper.updateCardUseResourceType(cardId, useResourceType);
    }
}
